/*!
 * 2015-07-19 08:39:32 GMT+0800 by @pomelo
 * 柚子pomelo 1.0 
 */


// expand: true 开启动态扩展
// cwd: 'images/' 源文件目录
// src: ['**/*.{gif,jpeg,jpg,png}'] 源文件格式
// dest: 'dist/images/' 输出目录
// **/* 多层
// {,*/}* 单层
// 缩进默认 2空格

'use strict';

module.exports = function (grunt) {

	// Load all grunt tasks automatically
	require('load-grunt-tasks')(grunt);

	// Time how long grunt task take. Can help when optimizing build times
	require('time-grunt')(grunt);

	// Configurable paths
	var config = {
		dist: 'dist'
	};

	grunt.initConfig({

		config: config,

		// Banner 注释
		banner: '/*!\n' +
			' * <%= grunt.template.today("yyyy-mm-dd HH:MM:ss Z") %> by @pomelo\n' +
			' * 柚子pomelo 1.0 \n' +
			' */',

		// 清空目录
		clean: {
			release: {
				src: ['<%= config.dist %>/', 'dist.zip']
			},
      sass: {
        src: ['sass/**/*.css','sass/**/*.map']
      }
		},

		// Sass
		sass: {
			options: {
				style: 'nested',
				sourcemap: false
			},
			style: {
				files: [
					{
						"css/main.css": 'sass/main.scss'
     }
        ]
			}
		},

		//compass
		compass: {
			dist: {
				config: 'config.rb',
				cssDir: 'css'
			}
		},

		// 打包发布
		usebanner: {
			dist: {
				options: {
					position: 'top',
					banner: '<%= banner %>'
				},
				files: {
					src: [
            '<%= config.dist %>/css/*.css',
            '<%= config.dist %>/js/*.js'
//						'gruntfile.js'
          ]
				}
			}
		},

		// 图片压缩
		imagemin: {
			dynamic: {
				options: {
					optimizationLevel: 3
				},
				files: [
					{
						expand: true,
						cwd: 'images/',
						src: ['**/*.{gif,jpeg,jpg,png}'],
						dest: '<%= config.dist %>/images/'
          }
        ]
			}
		},

		// 配合 livereload 使用，建立本地调试环境
		connect: {
			options: {
				port: 11011,
				hostname: 'localhost',
				base: '.',
				livereload: 35729
			},
			all: {
				options: {
					open: true,
					base: [
            '.'
          ]
				}
			}
		},
    
		//监控
		watch: {
			options: {
				spawn: false
			},
			style: {
				files: ['sass/**/*.scss'],
				tasks: ['compass'],
				options: {
					livereload: true
				}
			},
			js: {
				files: ['js/**/*.js'],
				tasks: ['jshint'],
				options: {
					livereload: true
				}
			},
			gruntfile: {
				files: ['gruntfile.js']
			},
			template: {
				files: ['include/**/*.html'],
				tasks: ['includereplace'],
				options: {
					livereload: true
				}
			},
			livereload: {
				files: [
//					"js/*.js",
//					"css/*.css"
        ],
				options: {
					livereload: '<%= connect.options.livereload %>'
				}
			}
		},

		// 配合打包发布
		copy: {
			dist: {
				src: [
          './images/**/*.*',
          'sprite/*.html',
          './css/media.css',
          './css/layer.css',
          './js/layer.js'
        ],
				dest: '<%= config.dist %>/'
			}
		},

		// css 属性排序
		csscomb: {
			dist: {
				files: {
					'<%= config.dist %>/css/main.css': 'css/main.css'
				}
			}
		},

		//模板
		includereplace: {
			user_index: {
				files: [
					{
						expand: true,
						cwd: './include/sprite',
						src: '*.html',
						dest: './sprite'
          }
        ]
			}
		},

		//html压缩
		htmlmin: {
			dist: {
				options: {
					collapseBooleanAttributes: false,
					collapseWhitespace: false,
					conservativeCollapse: false,
					removeAttributeQuotes: false,
					removeCommentsFromCDATA: false,
					removeEmptyAttributes: false,
					removeOptionalTags: false,
					removeRedundantAttributes: false,
					useShortDoctype: false
				},
				files: [{
					expand: true,
					cwd: '<%= config.dist %>',
					src: '{,*/}*.html',
					dest: '<%= config.dist %>'
        }]
			}
		},

		//路径替换（组合）
		useminPrepare: {
			options: {
				dest: '<%= config.dist %>'
			},
			html: [
        '<%= config.dist %>/**/*.html'
      ]
		},
		usemin: {
			options: {
				assetsDirs: [
          '<%= config.dist %>',
          '<%= config.dist %>/images',
          '<%= config.dist %>/css',
          '<%= config.dist %>/js'
        ]
			},
			html: ['<%= config.dist %>/**/*.html'],
			css: ['<%= config.dist %>/css/**/*.css']
		},

		//css压缩
		cssmin: {
			dist: {
				files: {
					'<%= config.dist %>/css/main.css': ['<%= config.dist %>/css/main.css']
				}
			}
		},

		//JS压缩
		uglify: {
			main: {
				files: {
					'<%= config.dist %>/js/main.js': ['<%= config.dist %>/js/main.js']
				}
			}
		},

		//文件合并
		concat: {
			concat_css: {
				files: [{
					src: [
            'css/main.css'
          ],
					dest: '<%= config.dist %>/css/main.css',
					nonull: true
        }]
			},
			concat_js: {
				files: [{
					src: [
            'js/main.js'
          ],
					dest: '<%= config.dist %>/js/main.js',
					nonull: true
        }]
			},
			concat_frozen: {
				files: [{
					src: [
            'js/zepto.min.js',
            'js/frozen.js'
          ],
					dest: '<%= config.dist %>/js/frozen.js',
					nonull: true
        }]
			}
		},

		//MD5
		rev: {
			dist: {
				files: {
					src: [
            '<%= config.dist %>/js/**/*.js',
            '<%= config.dist %>/css/**/*.css',
            '<%= config.dist %>/images/**/*.*',
            '<%= config.dist %>/*.{ico,png,jpg}'
          ]
				}
			}
		},

		//JS校验
		jshint: {
			options: {
				jshintrc: '.jshintrc',
				reporter: require('jshint-stylish')
			},
			all: [
        'gruntfile.js',
        'js/**/*.js'
      ]
		},

		//css校验
		csslint: {
			strict: {
				options: {
					import: 2
				},
				src: ['css/*.css']
			}
		},
		csslint_plus: {
			rules: [
        'node_modules/grunt-more-csslint-rules/examples/*.js'
      ]
		},

		//浏览器前缀
		autoprefixer: {
			options: {
				browsers: [
//          移动端CSS前缀
          'Android 2.3',
          'Android >= 4',
          'Chrome >= 20',
          'iOS >= 6'
            
//          PC版本CSS前缀
//          'ie >= 8',
//					'ie_mob >= 10',
//					'ff >= 30',
//					'chrome >= 34',
//					'safari >= 7',
//					'opera >= 23',
//					'ios >= 7',
//					'android >= 2.3',
//					'bb >= 10'
    ]
			},
			dist: {
				files: [{
					expand: true,
					src: '<%= config.dist %>/css/*.css'
        }]
			},
			css: {
				files: [{
					expand: true,
					src: 'css/main.css'
        }]
			}
		},

		//dist压缩
		zip: {
			'dist.zip': ['dist/js/*.js', 'dist/css/*.css', 'dist/sprite/*.html', 'dist/images/**/*']
		}

	});

  //监控
  	grunt.registerTask('', [
      'connect:all',
      'watch',
 ]);

  
  
  
	//自动刷新监控
	grunt.registerTask('server', [
    'connect:all',
    'watch',
  //		'csslint',
  //		'csslint_plus',
 ]);

	//打包
	grunt.registerTask('build', [
    'clean',
    'compass',
    'useminPrepare',
    'concat',
    'copy',
    'autoprefixer:dist',
    'imagemin',
    'cssmin',
    'uglify',
    'rev',
    'usemin',
    'usebanner',
//    'htmlmin'
    'zip'
  ]);
};